package com.medicine.web.controller.manage;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.medicine.common.annotation.Log;
import com.medicine.common.core.controller.BaseController;
import com.medicine.common.core.domain.AjaxResult;
import com.medicine.common.enums.BusinessType;
import com.medicine.manage.domain.Container;
import com.medicine.manage.service.IContainerService;
import com.medicine.common.utils.poi.ExcelUtil;
import com.medicine.common.core.page.TableDataInfo;

/**
 * 容器管理Controller
 *
 * @author admin
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/manage/container")
public class ContainerController extends BaseController
{
    @Autowired
    private IContainerService containerService;

    /**
     * 查询容器管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Container container)
    {
        startPage();
        List<Container> list = containerService.selectContainerList(container);
        return getDataTable(list);
    }

    /**
     * 导出容器管理列表
     */
    @Log(title = "容器管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Container container)
    {
        List<Container> list = containerService.selectContainerList(container);
        ExcelUtil<Container> util = new ExcelUtil<Container>(Container.class);
        util.exportExcel(response, list, "容器管理数据");
    }

    /**
     * 获取容器管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(containerService.selectContainerById(id));
    }

    /**
     * 新增容器管理
     */
    @Log(title = "容器管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Container container)
    {
        if(!containerService.checkUnique(container.getNumber(), container.getDeptId())){
            return error("新增容器'" + container.getNumber() + "'失败，容器编号已存在");
        }
        return toAjax(containerService.insertContainer(container));
    }

    /**
     * 修改容器管理
     */

    @Log(title = "容器管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Container container)
    {
        if(!containerService.checkUnique(container.getNumber(), container.getDeptId())){
            return error("更新容器'" + container.getNumber() + "'失败，容器编号已存在");
        }
        return toAjax(containerService.updateContainer(container));
    }

    /**
     * 删除容器管理
     */
    @PreAuthorize("@ss.hasPermi('manage:container:remove')")
    @Log(title = "容器管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(containerService.deleteContainerByIds(ids));
    }
}
